'''Support for BerkeleyDB 3.3 through 4.4 with a simple interface.
For the full featured object oriented interface use the bsddb.db module
instead. It mirrors the Sleepycat BerkeleyDB C API.
'''
try:
if __name__ == 'bsddb3':
import _pybsddb
_bsddb = _pybsddb
from bsddb3.dbutils import DeadlockWrap as _DeadlockWrap
else:
import _bsddb
from bsddb.dbutils import DeadlockWrap as _DeadlockWrap
except ImportError:
import sys
del sys.modules[__name__]
raise
db = _db = _bsddb
__version__ = db.__version__
error = db.DBError
import sys
import os
if sys.version >= '2.3':
import UserDict
from weakref import ref
exec "\nclass _iter_mixin(UserDict.DictMixin):\n def _make_iter_cursor(self):\n cur = _DeadlockWrap(self.db.cursor)\n key = id(cur)\n self._cursor_refs[key] = ref(cur, self._gen_cref_cleaner(key))\n return cur\n\n def _gen_cref_cleaner(self, key):\n # use generate the function for the weakref callback here\n # to ensure that we do not hold a strict reference to cur\n # in the callback.\n return lambda ref: self._cursor_refs.pop(key, None)\n\n def __iter__(self):\n try:\n cur = self._make_iter_cursor()\n\n # FIXME-20031102-greg: race condition. cursor could\n # be closed by another thread before this call.\n\n # since we're only returning keys, we call the cursor\n # methods with flags=0, dlen=0, dofs=0\n key = _DeadlockWrap(cur.first, 0,0,0)[0]\n yield key\n\n next = cur.next\n while 1:\n try:\n key = _DeadlockWrap(next, 0,0,0)[0]\n yield key\n except _bsddb.DBCursorClosedError:\n cur = self._make_iter_cursor()\n # FIXME-20031101-greg: race condition. cursor could\n # be closed by another thread before this call.\n _DeadlockWrap(cur.set, key,0,0,0)\n next = cur.next\n except _bsddb.DBNotFoundError:\n return\n except _bsddb.DBCursorClosedError:\n # the database was modified during iteration. abort.\n return\n\n def iteritems(self):\n if not self.db:\n return\n try:\n cur = self._make_iter_cursor()\n\n # FIXME-20031102-greg: race condition. cursor could\n # be closed by another thread before this call.\n\n kv = _DeadlockWrap(cur.first)\n key = kv[0]\n yield kv\n\n next = cur.next\n while 1:\n try:\n kv = _DeadlockWrap(next)\n key = kv[0]\n yield kv\n except _bsddb.DBCursorClosedError:\n cur = self._make_iter_cursor()\n # FIXME-20031101-greg: race condition. cursor could\n # be closed by another thread before this call.\n _DeadlockWrap(cur.set, key,0,0,0)\n next = cur.next\n except _bsddb.DBNotFoundError:\n return\n except _bsddb.DBCursorClosedError:\n # the database was modified during iteration. abort.\n return\n"
else:
class _iter_mixin:
pass
class _DBWithCursor(_iter_mixin):
'''
A simple wrapper around DB that makes it look like the bsddbobject in
the old module. It uses a cursor as needed to provide DB traversal.